package com.binya.lib.permission.pop;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.binya.lib.permission.R;
import com.binya.lib.permission.view.PermissionDesBean;
import com.binya.lib.permission.view.PermissionDesView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ydh
 * Time 2023年06月05日
 */
public class PermissionAuthReminderPop extends PopupWindow {

    Context mCxt;

    PermissionDesView permission_des;

    public PermissionAuthReminderPop(Context mContext) {
        mCxt = mContext;
        init();
    }

    //筛选弹窗
    @SuppressLint("ResourceType")
    private void init() {
        View contentView = LayoutInflater.from(mCxt).inflate(R.layout.pop_permission_auth_reminder, null);

        permission_des = contentView.findViewById(R.id.permission_des);

        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) mCxt, 1f);
            }
        });

//        setBackgroundDrawable(mCxt.getResources().getDrawable(Color.parseColor("#00000000")));

        setTouchable(true);
        // 设置外部可点击
        setOutsideTouchable(false);
        // 设置弹出窗体可点击
        setFocusable(true);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置视图
        setContentView(contentView);
    }

    public void showWin(View view, String permission) {
        showWin(view, new String[]{permission});
    }

    public void showWin(View view, String[] permission) {
        permission_des.showData(setPermissionTip(permission));

        showAtLocation(view, Gravity.TOP, 0, 0);
        backgroundAlpha((Activity) mCxt, 0.5f);
    }

    Comparable<String> comparator;

    public void setComparator(Comparable<String> comparator) {
        this.comparator = comparator;
    }

    /**
     * @param permission
     */
    private List<PermissionDesBean> setPermissionTip(String[] permission) {
        List<PermissionDesBean> list = new ArrayList<>();
        for (String s : permission) {
            PermissionDesBean bean = null;
            if (s.contains(("FINE_LOCATION"))) {
                bean = new PermissionDesBean();
                bean.setTitle(mCxt.getString(R.string.permission_auth_location_title));
                bean.setDes(mCxt.getString(R.string.permission_auth_location_des));
            } else if (s.contains(("WRITE_EXTERNAL_STORAGE"))) {
                bean = new PermissionDesBean();
                bean.setTitle(mCxt.getString(R.string.permission_auth_storage_title));
                bean.setDes(mCxt.getString(R.string.permission_auth_storage_des));
            } else if (s.contains(("CAMERA"))) {
                bean = new PermissionDesBean();
                bean.setTitle(mCxt.getString(R.string.permission_auth_camera_title));
                bean.setDes(mCxt.getString(R.string.permission_auth_camera_des));
            } else if (s.contains(("AUDIO"))) {
                bean = new PermissionDesBean();
                bean.setTitle(mCxt.getString(R.string.permission_auth_audio_title));
                bean.setDes(mCxt.getString(R.string.permission_auth_audio_des));
            } else if (s.contains(("PHONE"))) {
                bean = new PermissionDesBean();
                bean.setTitle(mCxt.getString(R.string.permission_auth_info_title));
                bean.setDes(mCxt.getString(R.string.permission_auth_info_des));
            }
            if (bean != null) {
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity mContext, float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

}
